﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Bounce : MonoBehaviour {
    public float bk = 1;
    private Vector3 m_preVelocity = Vector3.zero;//上一帧速度
    private Rigidbody rig;
    private void Awake()
    {
        rig = GetComponent<Rigidbody>();
    }
    private void Update()
    {
        m_preVelocity = rig.velocity;
    }

    private void OnCollisionEnter(Collision collision)
    {
        Rigidbody rig = GetComponent<Rigidbody>();
        Vector3 nor = collision.contacts[0].normal;
        //Debug.Log("nor "+nor);
        //Debug.Log("ve "+rig.velocity);
        float dot = Vector3.Dot(nor, m_preVelocity);
        //Debug.Log("dot" + dot);
        Vector3 add = -nor * 2 * bk*dot;
        //Debug.Log("add" + add);
        rig.velocity = m_preVelocity+ add;
    }
}
